<?php

/**
 * sugerencia actions.
 *
 * @package    tvpv
 * @subpackage sugerencia
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 8507 2008-04-17 17:32:20Z fabien $
 */
class sugerenciaActions extends sfActions
{
  public function executeIndex($request)
  {
    //$this->sugerenciaList = SugerenciaPeer::doSelect(new Criteria());
    $ppage = $this->getRequestParameter("page", 1);
    $this->articuloList = ArticuloPeer::getAll($ppage,$request->getParameter('sort'),$request->getParameter('find',null),$request->getParameter('find2',null),$request->getParameter('find3',null),$request->getParameter('find4',null));
    if($request->getParameter('find') == '')
      $request->setParameter('find',null);
    if($request->getParameter('find2') == '')
      $request->setParameter('find2',null);
    if($request->getParameter('find3') == '')
      $request->setParameter('find3',null);
    if($request->getParameter('find4') == '')
      $request->setParameter('find4',null);    
  }

  public function executeCreate()
  {
    $this->form = new SugerenciaForm();

    $this->setTemplate('edit');
  }

  public function executeEdit($request)
  {
    $this->form = new SugerenciaForm(SugerenciaPeer::retrieveByPk($request->getParameter('id')));
    //$this->form = SugerenciaPeer::getArticulo($request->getParameter('id'));
    //$tmp = $this->form->getObject();
    //$tmp->setRelacionado($request->getParameter('id'));
    $this->id = $request->getParameter('id');
    //$this->form['relacionado'] = $request->getParameter('id'); 
  }

  public function executeUpdate($request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new SugerenciaForm(SugerenciaPeer::retrieveByPk($request->getParameter('id')));

    $this->form->bind($request->getParameter('sugerencia'));
    if ($this->form->isValid())
    {
      $sugerencia = $this->form->save();

      $this->redirect('sugerencia/edit?id='.$sugerencia->getId());
    }
    else
      $this->logMessage("No valido))))))))))))");

    $this->setTemplate('edit');
  }

  public function executeDelete($request)
  {
    $this->forward404Unless($sugerencia = SugerenciaPeer::retrieveByPk($request->getParameter('id')));

    $sugerencia->delete();

    $this->redirect('sugerencia/index');
  }
  
  public function executeDelSugerencia($request){
  	$c = new Criteria();
  	$c->add(SugerenciaPeer::ARTICULO_ID,$request->getParameter('id'));
  	$c->add(SugerenciaPeer::RELACIONADO, $request->getParameter('rel'));
  	$sugerencia = SugerenciaPeer::doSelect($c);
  	$sugerencia[0]->delete();
  	$this->redirect('sugerencia/edit?id='.$request->getParameter('id'));
  }
  
  public function executeSetSugerencia($request){
  	$this->forward404Unless($request->isMethod('post'));
  	$sugerencia  = $request->getParameter('sugerencia');
  	$c  = new Criteria();
  	$c->add(SugerenciaPeer::ARTICULO_ID, $sugerencia['articulo_id']);
  	$c->add(SugerenciaPeer::RELACIONADO, $sugerencia['relacionado']);
  	if ( SugerenciaPeer::doCount($c) == 0 )
  	{
  		$sug = new Sugerencia();
  		$sug->setArticuloId($sugerencia['articulo_id']);
  		$sug->setRelacionado($sugerencia['relacionado']);
  		$sug->save();
  	}
  	//$this->form = new SugerenciaForm(SugerenciaPeer::retrieveByPk($request->getParameter('id')));
    //$this->form->bind($request->getParameter('sugerencia'));
    //if ($this->form->isValid())
    {
      //$sugerencia = $this->form->save();

      $this->redirect('sugerencia/edit?id='.$sugerencia['articulo_id']);
    }
  	$this->setTemplate('edit');
  }
}
